package com.zzw.junit5demo;

import org.junit.platform.commons.JUnitException;
import org.junit.platform.engine.ConfigurationParameters;
import org.junit.platform.engine.support.hierarchical.ParallelExecutionConfiguration;
import org.junit.platform.engine.support.hierarchical.ParallelExecutionConfigurationStrategy;

/**
 * @author zzw
 * @version 1.0
 * @title CustomStrategy
 * @description <自定义并行策略>
 * @datetime 2023-07-05 23:26
 */
public class CustomStrategy implements ParallelExecutionConfigurationStrategy {
    @Override
    public ParallelExecutionConfiguration createConfiguration(ConfigurationParameters configurationParameters) {
        int count =  configurationParameters.get("custom.parallelism", Integer::valueOf).orElseThrow(() ->
                new JUnitException(String.format("Configuration parameter '%s' must be set", "custom.parallelism")));

        return new ParallelExecutionConfiguration() {
            @Override
            public int getParallelism() {
                return count;
            }

            @Override
            public int getMinimumRunnable() {
                return count;
            }

            @Override
            public int getMaxPoolSize() {
                return count;
            }

            @Override
            public int getCorePoolSize() {
                return count;
            }

            @Override
            public int getKeepAliveSeconds() {
                return count;
            }
        };
    }
}
